package com.goktech.controller;

import com.goktech.entityDto.UserDto;
import com.goktech.pojo.Register;
import com.goktech.pojo.UpdatePass;
import com.goktech.pojo.User;
import com.goktech.service.UserService;
import com.goktech.utils.Result;
import com.goktech.utils.UserHolder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.goktech.utils.Constants.NOT_LOGIN;

/**
 * <p>
 * 用户实体类 前端控制器
 * </p>
 *
 * @author 初茶
 * @since 2023-05-14
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Result<String> login(@RequestBody User user) {
        return userService.login(user);
    }

    @PostMapping("/register")
    public Result<String> register(@RequestBody Register register) {
        return userService.register(register);
    }

    @PostMapping("/updatePass")
    public Result<String> updatePass(@RequestBody UpdatePass updatePass) {
        return userService.updatePass(updatePass);
    }

    @PostMapping("/updateUserInfo")
    public Result<String> updateUserInfo(@RequestBody User userInfo, HttpServletRequest request) {
        return userService.updateUserInfo(userInfo, request);
    }

    @PostMapping("/uploadHeader")
    public Result<String> uploadHeader(@RequestParam("file") MultipartFile headerFile) {
        return userService.uploadHeader(headerFile);
    }

    @RequestMapping("/me")
    public Result<UserDto> me() {
        if (ObjectUtils.isEmpty(UserHolder.getUser())) {
            return Result.fail(NOT_LOGIN);
        }
        return Result.ok(UserHolder.getUser());
    }

    @RequestMapping("/imageGenerate")
    public void imageGenerate(HttpServletResponse response) {
        userService.imageGenerate(response);
    }

    @RequestMapping("/updPassCode")
    public void updPassCode(@RequestParam("uId") Long uId, HttpServletResponse response) {
        userService.updPassCode(response, uId);
    }
}

